package com.qiudaozhang.jblog.backend.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.qiudaozhang.jblog.model.Tag;
import com.qiudaozhang.jblog.article.service.TagService;
import com.qiudaozhang.jblog.common.util.CommonCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("admin/tag")
public class TagController {


    @Reference(url = "${article.service.provider.url}", version = "${service.version}", group = "${dubbo.group}")
    private TagService tagService;

     @RequestMapping("index")
    public String index(){
        return "admin/tag/list";
    }

    /**
     * 分页查询所有数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam("page") int pageNum, @RequestParam("limit") int pageSize,
                                   @RequestParam(required = false,name = "name") String name){
        Map<String,Object> map = new HashMap<>();
        PageInfo<Tag> pageInfo =  tagService.queryPageParam(pageNum,pageSize,name);
//        pageInfo = tagService.queryPage(pageNum,pageSize);
        map.put("code",0);
        map.put("msg","");
        map.put("data",pageInfo.getList());
        map.put("count",pageInfo.getTotal());
        return map;
    }

    /**
     * 按照标签id删除标签
     * @param tagId
     * @return
     */
    @RequestMapping("delete/{tagId}")
    @ResponseBody
    public String delete(@PathVariable("tagId") Integer tagId){
        tagService.deleteById(tagId);
        return CommonCode.SUCCESS;
    }

    /**
     * 更新标签信息
     * @param tag
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public String update(@RequestBody Tag tag){
        tagService.update(tag);
        return CommonCode.SUCCESS;
    }



    @RequestMapping("post")
    @ResponseBody
    public String post(@RequestBody Tag tag){
        tagService.save(tag);
        return CommonCode.SUCCESS;
    }
}












